package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Vote;
import com.hongkuncheng.cms.model.VoteOption;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 网上投票
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/vote")
public class AvoteController extends BaseController {

    // 网上投票
    @RequestMapping
    public String index() {
        Vote vote = factorySession.get(Vote.class, 1);
        request.setAttribute("vote", vote);
        List<VoteOption> voteoptions = factorySession.createCriteria(VoteOption.class)
                .addOrder(Order.asc("sort")).list();
        request.setAttribute("voteoptions", voteoptions);
        int polltotal = 0;
        for (VoteOption voteoption : voteoptions) polltotal += voteoption.getPoll();
        request.setAttribute("polltotal", polltotal);
        return view();
    }

    // 投票主题更新
    @RequestMapping("update")
    @ResponseBody
    public void update() {
        checkAdminPermission("yunwei_plugin_vote");
        Vote vote = factorySession.load(Vote.class, 1, LockMode.PESSIMISTIC_WRITE);
        vote.setTitle(request.getParameter("title"));
        PageHelper.alertAndRedirect("更新成功", "./");
    }

}
